﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using JGIC.CommandLine.Utility;

using System.Collections.Specialized;
using System.Text.RegularExpressions;

namespace rtf2txt
{
    //public enum lll
    //    {
    //        tstt,
    //        aaaa,
    //        fff
    //    }
    //public class tst 
    //{
    //    enum MyEnum
    //{
    //        sgdfg,sdfgdfg,sdfgdf
    //}
    //     public enum lll
    //    {
    //        tstt,
    //        aaaa,
    //        fff
    //    }
    //    public void lll()
    //    {

    //    }

    //}
    class Program
    {

        static void Main(string[] args)
        {
            //tst t = new tst();
            //t.lll.aaaa = tst.lll.

            //
            // 1. The input Rtf to standard output
            // 2. The input Rtf to file output

            Arguments CommandLine = new Arguments(args);
            bool d = false;


            if (CommandLine["verbose"] != null | CommandLine["v"] != null)
            {
                d = true; //debug
            }
            bool stdout = false;


            #region set variables from args

            #region help...

            bool help = false; // for the help CLI
            if (CommandLine["help"] != null | CommandLine["h"] != null | args[0] == "/?" | args[0] == "/help" | args[0] == "/h") help = true;
            #endregion
            String fileOut = "";
            String fileIn = "";
            bool Overwriting = false;

            if (CommandLine["stdout"] != null) stdout = true;
            if (CommandLine["s"] != null) stdout = true;
            if (CommandLine["fileout"] != null) fileOut = CommandLine["fileout"];
            if (CommandLine["o"] != null) fileOut = CommandLine["o"];
            if (CommandLine["file"] != null) fileIn = CommandLine["file"];
            if (CommandLine["f"] != null) fileIn = CommandLine["f"];
            if (CommandLine["overwrite"] != null) Overwriting = true;
            if (CommandLine["w"] != null) Overwriting = true;


            #endregion
            Jgi.Tools.JgiAsciiConverter cv = new Jgi.Tools.JgiAsciiConverter();



            if (fileIn == "" | (args.Length < 1) | help == true)
            {
                Console.WriteLine("\nRtf2Text Jgi.Tools.CLi Utilities\nVersion 1.0.0.1.2009.07.25\nby Jean-Guillaume Isabelle (2009)\n\nUsage:\n\n"
                    + "--file, -f [filename]\n"
                    + "--stdout, -s (output to standard) (Default output)\n"
                    + "--xml, -x (output in XML the formatting) (NOT IMPLEMENTED YET)\n"
                    + "--fileout, -o [filename]\n"
                    + "--verbose, -v (verbose info printed)\n"
                    + "--overwrite, -w (Overwrite existing file (default won't))\n"
                    );
            }
            #region else

            #endregion
            else
            {

                String file;
                file = fileIn;
                if (d) Console.WriteLine("file: " + file + "\n");
                String c;

                if (stdout | fileOut == "")
                {
                    if (d) Console.WriteLine("\nThis: "
                    + " if (CommandLine[\"stdout\"] != null & CommandLine[\"s\"] != null)"
                    + " PASSED...entering\n");

                    c = cv.LoadFileContent(fileIn);
                    if (d) Console.WriteLine("c: " + c + "\n");

                    Console.WriteLine(cv.ConverterRtf2Text(c));
                }

                if (fileOut != "")
                {
                    if (d) Console.WriteLine("c: " + CommandLine["fileout"] + "!= null | CommandLine[\"o\"]" + "\n");

                    if (File.Exists(fileOut) & Overwriting == true)
                    {
                        cv.LoadAndSerializeContent(fileIn, fileOut);
                    }
                    else
                    {
                        Console.WriteLine("\nError\nFile exist\n\nUse: --overwrite, -w\n\nquitting...");
                    }
                }

            }

            #region file IO Tests

            //File.WriteAllText("tst_.txt","whatever");
            //cv.LoadAndSerializeContent(fileIn, "tst.txt",true);

            #endregion
        }


    }
}
namespace Jgi.Tools
{
    public class JgiAsciiConverter
    {

        /// <summary>
        /// Take Rtf as input and return Text
        /// </summary>
        /// <param name="Rtf"></param>
        /// <returns>Rtf content</returns>
        public String ConverterRtf2Text(String Rtf)
        {
            System.Windows.Forms.RichTextBox richtextbox = new System.Windows.Forms.RichTextBox();
            richtextbox.Rtf = Rtf;
            return richtextbox.Text;
        }


        public String LoadFileContent(String path)
        {
            if (File.Exists(path))
            {
                String content = File.ReadAllText(path);
                return content;
            }
            else
            {
                Console.WriteLine("Failed loading");
                return "Failed";
            }
        }

        public void LoadAndSerializeContent(String fileIn, String fileOut)
        {
            String c = ConverterRtf2Text(LoadFileContent(fileIn));
            SerializeFile(fileOut, c);
        }


        //humm...useless
        public void LoadAndSerializeContent(String fileIn, String fileOut, bool Overwritepath)
        {
            String c = LoadFileContent(fileIn);
            SerializeFile(fileOut, c, Overwritepath);
        }


        public void SerializeFile(String path, String content)
        {
            try
            {
                File.WriteAllText(path, content);
            }
            catch (Exception e)
            {
                Console.WriteLine("\nError " + e.ToString() + "\n\n\nSomething when wrong...sorry\n");
                throw;
            }

        }


        //useless
        public void SerializeFile(String path, String content, bool Overwritepath)
        {
            try
            {
                if (File.Exists(path))
                {
                    File.WriteAllText(path, content);
                }
                else
                {
                    File.WriteAllText(path, content);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("\nError " + e.ToString() + "\n\n\nSomething when wrong...sorry\n");
                throw;
            }

        }
    }
}
